const HOOKS = [
  "beforeCreate",
  "created",
  "beforeMount",
  "mounted",
  "beforeUpdate",
  "updated",
  "beforeDestory",
  "destroyed"
]
// 策略模式
let starts = {}
starts.data = function (parentVal, childVal) {
  // return childVal
} //合并data
starts.computed = function () { } //合并computed
starts.watch = function () { } //合并watch 
starts.methods = function () { } //合并methods 
//遍历生命周期
// 合并生命周期方法就是把各自的所有方法都存入同一个数组内 比如{ created: [createdA, createdB...] }
HOOKS.forEach(hooks => {
  starts[hooks] = mergeHook;
})
function mergeHook(parentVal, childVal) {
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal);
    } else {
      return [childVal];
    }
  } else {
    return parentVal;
  }
}

export function mergeOptions(parent, child) {
  const options = {}
  for (let key in parent) {
    mergeField(key)
  }
  for (let key in child) {
    mergeField(key)
  }
  function mergeField(key) {
    //根据key   策略模式
    if (starts[key]) {
      options[key] = starts[key](parent[key], child[key])
    } else {
      options[key] = child[key]
    }
  }
  return options
}